package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.qy.dao.DriverFinanceDao;
import java.util.List;
import java.util.Map;
import java.sql.Types;
import com.qy.db.DbParameter;
import java.util.HashMap;
import java.util.ArrayList;
import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.DriverFinance;
import com.qy.db.BaseDao;

/**
* u_driver_finance daoImpl实现类的生成
* @author zhangwenlong
* @date Sat Feb 10 14:49:29 GMT+08:00 2018
*/ 
public class DriverFinanceDaoImpl extends BaseDao  implements DriverFinanceDao {

	@Override
	public boolean createDriverFinance(DriverFinance driverFinance) {
		boolean result = false;
		int id = -1;
		if (driverFinance.beginAdd()) {
			String sql = "insert into u_driver_finance (id,driver_id,price,state,create_time,audit_time,audit_remarks) "
				+ "values (?,?,?,?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, driverFinance.getId()));
			param.put(2, new DbParameter(Types.INTEGER, driverFinance.getDriverId()));
			param.put(3, new DbParameter(Types.INTEGER, driverFinance.getPrice()));
			param.put(4, new DbParameter(Types.INTEGER, driverFinance.getState()));
			param.put(5, new DbParameter(Types.TIMESTAMP, driverFinance.getCreateTime()));
			param.put(6, new DbParameter(Types.TIMESTAMP, driverFinance.getAuditTime()));
			param.put(7, new DbParameter(Types.VARCHAR, driverFinance.getAuditRemarks()));
			id = execLastId(sql, param);
			result = id > -1;
			driverFinance.commitAdd(result);
			driverFinance.setId(id);
		}
		return result;
	}

	@Override
	public List<DriverFinance> getDriverFinanceList(int userId) {
		String sql = "select * from u_driver_finance where driver_id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, userId));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		List<DriverFinance> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<DriverFinance>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public DriverFinance getDriverFinanceById(int id) {
		String sql = "select * from u_driver_finance where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		DriverFinance driverFinance = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					driverFinance = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return driverFinance;
	}

	@Override
	public boolean updateDriverFinance(DriverFinance driverFinance) {
		boolean result = false;
		if (driverFinance.beginUpdate()) {
			String sql = "update u_driver_finance set driver_id=?,price=?,state=?,create_time=?,audit_time=?,audit_remarks=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, driverFinance.getDriverId()));
			param.put(2, new DbParameter(Types.INTEGER, driverFinance.getPrice()));
			param.put(3, new DbParameter(Types.INTEGER, driverFinance.getState()));
			param.put(4, new DbParameter(Types.TIMESTAMP, driverFinance.getCreateTime()));
			param.put(5, new DbParameter(Types.TIMESTAMP, driverFinance.getAuditTime()));
			param.put(6, new DbParameter(Types.VARCHAR, driverFinance.getAuditRemarks()));
			param.put(7, new DbParameter(Types.INTEGER, driverFinance.getId()));
			result = execNoneQuery(sql, param) > -1;
			driverFinance.commitUpdate(result);
		}
		return result;
	}

	public DriverFinance resultTo(ResultSet rs) throws SQLException {
		DriverFinance driverFinance = new DriverFinance();
		driverFinance.setId(rs.getInt("id"));
		driverFinance.setDriverId(rs.getInt("driver_id"));
		driverFinance.setPrice(rs.getInt("price"));
		driverFinance.setState(rs.getInt("state"));
		driverFinance.setCreateTime(rs.getTimestamp("create_time"));
		driverFinance.setAuditTime(rs.getTimestamp("audit_time"));
		driverFinance.setAuditRemarks(rs.getString("audit_remarks"));
		driverFinance.setOp(Option.None);
		return driverFinance;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}

}

